Public Class PopupCalendar
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents calDate As System.Web.UI.WebControls.Calendar
    Protected WithEvents DropDownList1 As System.Web.UI.WebControls.DropDownList
    Protected WithEvents DropDownList2 As System.Web.UI.WebControls.DropDownList
    Protected WithEvents Button1 As System.Web.UI.WebControls.Button
    Protected WithEvents control As System.Web.UI.HtmlControls.HtmlInputHidden

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        If Not IsPostBack Then
            control.Value = Request.QueryString("TEXTBOX")
            Me.LoadYear()
        End If
    End Sub

    Protected Sub Change_Date(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Session("Change") = "Y"
        Dim strScript As String = "<script>window.opener.document.forms(0)." + control.Value + ".value = '"
        strScript += Format(calDate.SelectedDate.Day, "00") & "/" & Format(calDate.SelectedDate.Month, "00") & "/" & CInt(IIf(calDate.SelectedDate.Year > 2500, calDate.SelectedDate.Year, calDate.SelectedDate.Year + 543)) 'calDate.SelectedDate.ToString("dd/MM/yyyy")
        'strScript += "';window.opener.RefreshForm();"
        strScript += "';self.close(); "
        strScript += "</" + "script>"
        RegisterClientScriptBlock("anything", strScript)
    End Sub
    Sub LoadYear()
        Dim x As Integer
        Dim i As Integer
        Dim d As Integer = Month(Now)
        Dim y As Integer = Year(Now)
        Dim sScript As String = ""
        DropDownList2.Items.Clear()
        i = 0
        For x = Year(Now) - 10 To Year(Now)
            If Not (IsPostBack) Then
                DropDownList2.Items.Add(CStr(x))
                If (x < 2500) Then '(x < 2500)
                    DropDownList2.Items(i).Text = CStr(x + 543)
                    DropDownList2.Items(i).Value = CStr(x)
                Else
                    DropDownList2.Items(i).Text = CStr(x)
                    DropDownList2.Items(i).Value = CStr(x - 543)
                End If
            End If
            i = i + 1
        Next
        DropDownList1.SelectedIndex = d - 1
        If y < 2500 Then
            DropDownList2.Items.FindByValue(CStr(y)).Selected = True
        Else
            DropDownList2.Items.FindByValue(CStr(y - 543)).Selected = True
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        calDate.VisibleDate = New DateTime(CInt(DropDownList2.SelectedItem.Value), CInt(DropDownList1.SelectedItem.Value), 1)
    End Sub

    Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
        calDate.VisibleDate = New DateTime(CInt(DropDownList2.SelectedItem.Value), CInt(DropDownList1.SelectedItem.Value), 1)
    End Sub

    Private Sub DropDownList2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DropDownList2.SelectedIndexChanged
        calDate.VisibleDate = New DateTime(CInt(DropDownList2.SelectedItem.Value), CInt(DropDownList1.SelectedItem.Value), 1)
    End Sub


End Class
